<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const orders = [
        {
          id: 1,
          name: 'John Smith',
          address: '123 Main St',
          status: 'created',
          log: [
            {
              status: 'created',
              time: '2023-02-16 12:30:00',
            },
          ],
        },
        {
          id: 2,
          name: 'Jane Doe',
          address: '456 Second St',
          status: 'packed',
          log: [
            {
              status: 'created',
              time: '2023-02-16 10:00:00',
            },
            {
              status: 'packed',
              time: '2023-02-16 12:30:00',
            },
          ],
        },
        {
          id: 3,
          name: 'Bob Johnson',
          address: '789 Third St',
          status: 'shipped',
          log: [
            {
              status: 'created',
              time: '2023-02-15 10:00:00',
            },
            {
              status: 'packed',
              time: '2023-02-15 12:00:00',
            },
            {
              status: 'shipped',
              time: '2023-02-16 9:00:00',
            },
          ],
        },
      ]
      //getOrdersByStatus(status: string)**: 该函数接受一个参数**status**，返回一个包含所有状态为**status**的订单的数组
      let packed = 'packed'
      let created = 'created'
      let shipped = 'shipped'
      function getOrdersByStatus(status) {
        const transit = JSON.stringify(orders)
        const copy = JSON.parse(transit)
        let sift = copy.map((item) => {
          let res = item.log.filter((item) => {
            return item.status === status
          })
          item.log = res
          return item
        })
        sift = sift.filter((item) => item.log.length > 0)
        sift.sort((a, b) => {
          return a.id - b.id
        })
        return sift
      }
      // console.log(getOrdersByStatus(created))
      // console.log(getOrdersByStatus(packed))
      // console.log(getOrdersByStatus(shipped))
      //getOrderStatusHistory(id: number)**: 该函数接受一个参数**id**，返回一个字符串，表示编号为**id**的订单的状态历史记录
      function getOrderStatusHistory(id) {
        const transit = JSON.stringify(orders)
        const copy = JSON.parse(transit)
        const res = copy
          .find((item) => item.id === id)
          .log.map((item) => {
            const temp = `[${item.time}]${item.status}`
            return temp
          })
        return res.join('\n')
      }
      // console.log(getOrderStatusHistory(2))
      //方法2
      function pro(status) {
        const arr = orders.filter((item) => item.status === status)
        const newArr = arr.map((item) => {
          return {
            id: item.id,
            name: item.name,
            address: item.address,
            status: item.status,
            log: item.log.filter((item) => {
              item.status === status
            }),
          }
        })
        return newArr
      }
      console.log(pro('created'))
      console.log(pro('packed'))
      console.log(pro('shipped'))
    </script>
  </body>
</html>
